/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月31日         luwei
 */

package com.coocaa.launcher.sidebar.left;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.coocaa.launcher.framework.sidebar.item.ItemView;
import com.coocaa.x.uipackage.SkinManager;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.widget.AlwaysMarqueeTextView;
import com.tianci.appstore.utils.SkyConstants;

public abstract class SideItemView extends ItemView {
    protected TextView mItemName = null;
    protected View mRightView = null;
    protected ImageView mIcon = null;

    public SideItemView(Context context) {
        super(context);
        addIcon();
        addItemName();
        addExtraView();
        //SkinManager.getInstance().setBackgroundResource(this, R.drawable.launcher_leftside_selector);
    }

    private void addIcon() {
        mIcon = new ImageView(mContext);
        LayoutParams params = new LayoutParams(SkyConstants.Div(40), SkyConstants.Div(40), Gravity.LEFT | Gravity.CENTER_VERTICAL);
        params.leftMargin = getLeftMargin();
        addView(mIcon, params);
    }

    private void addItemName() {
        mItemName = new AlwaysMarqueeTextView(mContext);
        mItemName.setTextAppearance(mContext, R.style.left_side_bar_txt_style);
        mItemName.setTextSize(SkyConstants.Dpi(38));
        mItemName.setText(getItemName());
        mItemName.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
        mItemName.setPadding(SkyConstants.Div(51) + SkyConstants.Div(36) + SkyConstants.Div(30), 0, SkyConstants.Div(30), 0);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, SkyConstants.Div(75));
        addView(mItemName, params);
    }

    public void updateIcon(int id) {
        SkinManager.getInstance().setBackgroundResource(mIcon, id);
    }

    public void updateName(int id) {
        mItemName.setText(id);
    }

    private void addExtraView() {
        mRightView = getRightView();
        if (mRightView != null) {
            if (mRightView.getLayoutParams() == null) {
                LayoutParams params = new LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
                params.rightMargin = SkyConstants.Div(30);
                addView(mRightView, params);
            } else {
                addView(mRightView);
            }
        }
    }

    public void hideRightView() {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if (mRightView != null) {
                    mRightView.setVisibility(View.GONE);
                }
            }
        });
    }

    public void showRightView() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mRightView != null) {
                    mRightView.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        mItemName.setSelected(hasFocus);
        super.onFocusChange(v, hasFocus);
    }

    public abstract String getItemName();

    public abstract int getItemIconId();

    public abstract View getRightView();

    public int getLeftMargin() {
        return SkyConstants.Div(51);
    }
}
